package com.cango.student.http;

import lombok.extern.log4j.Log4j2;
import okhttp3.*;
import okio.BufferedSink;

import javax.annotation.Nullable;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;

/**
 * @program: student
 * @description:
 * @author: xie jiefeng
 * @create: 2019-10-16 10:52
 */
public class OkHttpTemplate {
    public static void main(String[] args) throws IOException,InterruptedException {
//        get();
//        getAsync();
        post();
    }

    public static void get() throws IOException {
        String url = "http://openapi-sandbox.sandload.cn/agent/check";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .get()  //默认为GET请求，可以不写
                .build();
        final Call call = client.newCall(request);
        Response response = call.execute();
        System.out.println(new String(response.body().bytes()));
    }

    public static void getAsync() throws IOException, InterruptedException {
        String url = "http://openapi-sandbox.sandload.cn/agent/check";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .get()  //默认为GET请求，可以不写
                .build();
        CountDownLatch latch = new CountDownLatch(1);
        final Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                System.out.println("onFailure: " + e);
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(Thread.currentThread().getName());
                System.out.println("OnResponse: " + new String(response.body().bytes()));
                latch.countDown();
            }
        });
        latch.await();
        System.out.println(Thread.currentThread().getName());
        System.exit(-1);
    }

    public static void post() throws IOException, InterruptedException {
        String url = "http://openapi-sandbox.sandload.cn/storecenter/bannertemplet/v2/get";
        OkHttpClient client = new OkHttpClient();
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"),"{\"partnerId\":2261,\"id\":\"1234\"}");
        Request request = new Request.Builder()
                .url(url)
                .post(body)
                .build();

        CountDownLatch latch = new CountDownLatch(1);

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
            }
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                System.out.println(Thread.currentThread().getName());
                System.out.println("OnResponse: " + new String(response.body().bytes()));
                latch.countDown();
            }
        });

        latch.await();
        System.out.println(Thread.currentThread().getName());
        System.exit(-1);

    }
}
